package com.wchc.admin.user.domain.service;

import com.wchc.admin.user.domain.repository.UserRepository;
import org.springframework.stereotype.Service;

import java.util.Optional;
import java.util.concurrent.CompletableFuture;

@Service
public class UserDomainService {
    
    private final UserRepository userRepository;
    
    public UserDomainService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }
    
    public CompletableFuture<User> authenticate(String username, String password) {
        return userRepository.findByUsername(username)
                .thenApply(userOpt -> userOpt.filter(user -> user.getPassword().equals(password))
                        .orElseThrow(() -> new RuntimeException("Invalid credentials")));
    }
    
    public CompletableFuture<Boolean> isUsernameAvailable(String username) {
        return userRepository.findByUsername(username)
                .thenApply(Optional::isEmpty);
    }
}